<?php
class BuyModel extends Db {
	
	private $_goods = 'd_buy_goods';
	
	//新增产品
	public function addGoods($params) {
		return $this->add ( $this->_goods, $params );
	}
	
	public function getGoodsByWhere($start, $num, $where) {
		return $this->getPage ( $start, $num, $this->_goods, null, $where, 'sort DESC' );
	}
	
	public function getGoodsByName($name) {
		return $this->getOne ( $this->_goods, array ('goods_name' => $name ) );
	}
	
	public function getGoodsByCid($start, $num, $cid) {
		return $this->getGoodsByWhere ( $start, $num, array ('cid' => $cid ) );
	}
	
	public function getGoodsByType($start, $num, $type) {
		return $this->getGoodsByWhere ( $start, $num, array ('type' => $type ) );
	}
	
	public function getGoodsKBBySales($limit) {
		$sql = "select * from d_buy_goods WHERE cid IN (2,3,4,6,7,12,14,15,16,17,19,22,23,24,25,26,28,30,31,32,33,34,38) order by sales DESC LIMIT $limit";
		return $this->fetchAll ( $sql );
	}
	
	public function getGoodsLikeTitle($title) {
		$sql = "SELECT * FROM $this->_goods WHERE goods_name LIKE \"%$title%\" LIMIT 20";
		return $this->fetchAll ( $sql );
	}
	
	public function getGoodsById($id) {
		return $this->getOne ( $this->_goods, array ('id' => $id ) );
	}
	
	public function setGoodsById($v, $id) {
		return $this->update ( $this->_goods, $v, array ('id' => $id ) );
	}
	
	public function getGoodsByRecommend() {
		return $this->getAll ( $this->_goods, array ('recommend' => 1 ) );
	}
	
	public function setGoodsSalesById($num, $id) {
		return $this->update ( $this->_goods, array ('sales' => $num ), array ('id' => $id ) );
	}
	
	public function getGoodsByRemark($remark) {
		return $this->getAll ( $this->_goods, array ('remark' => $remark ) );
	}
	
	/**
	 * 获取产品分类
	 */
	public function getGoodsCategory() {
		$sql = "select distinct(category) from d_buy_goods group by category";
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 
	 * @return BuyModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}

}